package com.fram.common.util.base;

import org.springframework.beans.factory.annotation.Autowired;

import com.fram.common.auth.UserAuthMethod;
import com.fram.common.auth.UserAuthMethod.LoginRedisKey;
import com.fram.common.exception.ProException;
import com.fram.common.exception.ProValiDataException;
import com.fram.common.util.PublicMethod;
import com.fram.mapper.DistributorFarmMapper;

/**
 * @作者： 陳曉鬆 @时间： 2018年3月8日 - 下午2:54:24 @description：->
 */
public class BaseController {
	
	@Autowired
	private DistributorFarmMapper distributorFarmMapper;
	
	/** 获取userId **/
	public Long getApiUserId() throws ProException {
		return UserAuthMethod.getLoginRedisKeyClass(PublicMethod.getRequest().getHeader(UserAuthMethod.LOGIN_TOKEN_NAME)).getUserId();
	}
	/** 获取分销商id **/
	public Long getDistributorId() throws ProException {
		LoginRedisKey loginRedisKey = UserAuthMethod.getLoginRedisKeyClass(PublicMethod.getRequest().getHeader(UserAuthMethod.LOGIN_TOKEN_NAME));
		Long distributorId = distributorFarmMapper.selectByUserId(loginRedisKey.getUserId());
		if(distributorId == null || distributorId == 0L) {
			throw new ProValiDataException("该用户没有店铺，等待申请通过！！！");
		}
		return distributorId;
	}
	/** 获取LoginRedisKey **/
	public String getApiLoginRedisKey() throws ProException {
		return UserAuthMethod.getLoginRedisKeyClass(PublicMethod.getRequest().getHeader(UserAuthMethod.LOGIN_TOKEN_NAME)).getRedisKey();
	}

}
